package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.utils.RequestUtils;
import com.atguigu.gmall.product.entity.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

@RestController
@RequestMapping("/api/inner/rpc/cart")
public class CartRpcController {

    @Autowired
    CartService cartService;

    /**
     * 把商品添加到购物车
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/add/{skuId}")
    public Result<SkuInfo> addToCart(@PathVariable("skuId") Long skuId,
                                     @RequestParam("skuNum") Integer skuNum,
                                     HttpServletRequest request) {

        //SkuInfo skuInfo = cartService.addToCart(skuId);
        //1.获取用户的临时id和用户id
        // Long uid = RequestUtils.uid();
        //String tempId = RequestUtils.userTempId();
        //2.得到这个用户的cartKey,知道是哪个用户在操作
        String cartKey = cartService.determinCartKey();
        //3.吧一个商品加到购物车
        SkuInfo skuInfo = null;
        skuInfo = cartService.addItemToCart(skuId, skuNum, cartKey);
        return Result.ok(skuInfo);
    }

    /**
     * 删除选中的数据
     * @return
     */
    @GetMapping("/delete/checked")
    public Result deleteChecked(){

        String cartKey = cartService.determinCartKey();
        cartService.deleteChecked(cartKey);
        return Result.ok();
    }
}
